import { Component, OnInit } from '@angular/core';
import {Udg} from "../../model/udg";
import {Http} from "@angular/http";
import {UserInfoService} from "../../service/user-info.service";

@Component({
  selector: 'app-udg-table',
  templateUrl: './udg-table.component.html',
  styleUrls: ['./udg-table.component.css']
})
export class UdgTableComponent implements OnInit {

  //选择人员分页
  _current = 1;
  _pageSize = 10;
  _total = 1;
  _dataSet = [];
  queryId = "";
  queryName = "";
  type:string;

  //选择框
  _allChecked = false;
  // _indeterminate = false;
  _displayData: Array<any> = [];
  checkAllUDG:Udg[] = [];

  constructor(
    private http: Http,
    private userInfoService:UserInfoService
  ) { }

  ngOnInit() {
    this.type = "U";
    this._allChecked = false;
    $("#openwin_querytype").val(this.type);
    this.userInfoService.getUserInfo(this._current,this._pageSize,this.queryId,this.queryName).then(res=>{
      this._dataSet = res.list;
      this._total = res.total;
      this._displayData = res.list;
      this.select(this.checkAllUDG,this._dataSet);
      //全选判断
      this._refreshStatus("page","");
    });
  }

  refreshData(reset = false) {
    if (reset) {
      this._current = 1;
    }
    // this._allChecked = false;
    this.dataByType(this._current);
  };

  _displayDataChange($event) {
    this._displayData = $event;
  };

  _checkAll(value) {
    if (value) {
      this._displayData.forEach(data => data.checked = true);
    } else {
      this._displayData.forEach(data => data.checked = false);
      for(let i=0;i<this._displayData.length;i++){
        this.checkAllUDG = this.checkAllUDG.filter((r) => !(r.id === this._displayData[i].id && r.type === this.type));
      }
    }
    this._refreshStatus("","");
  };

  //当前页checkbox选中判断
  _refreshStatus(event,id) {
    if(this._displayData.length>0){
      let flag = true;
      for(let i =0;i<this._displayData.length;i++){
        if(this._displayData[i].checked!=true){
          flag = false;
          break;
        }
      }
      if(flag){
        this._allChecked = true;
        for(let i=0;i<this._dataSet.length;i++){
          //去掉重复选择数据
          this.checkAllUDG = this.checkAllUDG.filter((r) => !(r.id === this._displayData[i].id && r.type === this.type));
          let selectInfo = this._dataSet.filter(value => value.id === this._dataSet[i].id);
          this.checkAllUDG.push({
            type:this.type,
            id : selectInfo[0].id,
            id_name : selectInfo[0].id+"_"+selectInfo[0].name,
            flag_id:selectInfo[0].type+"_"+selectInfo[0].id
          });
        }
      }else{
        this._allChecked = false;
        //去掉重复选择数据
        if(event==true){
          this.checkAllUDG = this.checkAllUDG.filter((r) => !(r.id === id && r.type === this.type));
          let selectInfo = this._dataSet.filter(value => value.id == id);
          this.checkAllUDG.push({
            type:this.type,
            id : selectInfo[0].id,
            id_name : selectInfo[0].id+"_"+selectInfo[0].name,
            flag_id:selectInfo[0].type+"_"+selectInfo[0].id
          });
        }
        if(event == false){
          this.checkAllUDG = this.checkAllUDG.filter((r) => !(r.id === id && r.type === this.type));
        }
      }
    }
  };

  //选择判断
  select(selectedData:any,currentData:any){
    let selectInfo = [];
    if(selectedData){
      let ids = [];
      for(var i=0;i<selectedData.length;i++){
        let data = selectedData[i].flag_id.split("_");
        if(data[0] == this.type){
          ids[ids.length] = data[1];
        }
      }
      for(var i=0;i<currentData.length;i++){
        for(var j=0;j<ids.length;j++){
          if(currentData[i].id == ids[j]){
            selectInfo.push(currentData[i]);
          }
        }
      }
      selectInfo.forEach(data => data.checked = true);
    }
  }

  typeChange(){
    this.clear();
    this.type = $("#openwin_querytype").val() as string;
    this._current = 1;
    this.dataByType(this._current);
  }

  //根据条件查找
  search() {
    this.queryId = $('#searchId').val() as string;
    this.queryName = $('#searchName').val() as string;
    this._current = 1;
    this.dataByType(this._current);
  }

  //类型数据判断
  dataByType(currentPage:number){
    if(this.type == "U"){
      this.userInfoService.getUserInfo(currentPage,this._pageSize,this.queryId,this.queryName).then(res=>{
        this._dataSet = res.list;
        this._total = res.total;
        this._displayData = res.list;
        this.select(this.checkAllUDG,this._dataSet);
        //全选判断
        this._refreshStatus("page","");

      });
    }else if(this.type == "D"){
      this.userInfoService.getDepartmentInfo(currentPage,this._pageSize,this.queryId,this.queryName).then(res=>{
        this._dataSet = res.list;
        this._total = res.total;
        this._displayData = res.list;
        this.select(this.checkAllUDG,this._dataSet);

        //全选判断
        this._refreshStatus("page","");
      });
    }else{
      this.userInfoService.getGroupInfo(currentPage,this._pageSize,this.queryId,this.queryName).then(res=>{
        this._dataSet = res.list;
        this._total = res.total;
        this._displayData = res.list;
        this.select(this.checkAllUDG,this._dataSet);

        //全选判断
        this._refreshStatus("page","");
      });
    }
  }

  //清空查询条件
  clear(){
    this.queryId = "";
    this.queryName = "";
    $("#searchId").val("");
    $("#searchName").val("");
  }

}
